package org.itboys.mobile.controller.weixin.pay;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.mysql.weixin.pay.SundayWeixinPayService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.pay.weixin.JsPaySignTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/7
 * 描述：迈迪龙_微信_支付
 */
@Controller
@RequestMapping("/sunday/weixin/pay")
public class SundayWeixinPayController extends BaseController{
    @Autowired
    private SundayWeixinPayService payService;

    /**
     * 进入支付页面
     * @param request
     * @param model
     * @param orderIds
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model,
            @RequestParam(value = "orderIds", required = true)List<Long> orderIds
    ){

        Map<String,Object> orderPayResult = payService.index(orderIds);
        model.addAttribute("orderPayResult",orderPayResult);
        return "/maidilong/weixin/pay/index";
    }
    /**
     * 微信-网页-统一下单
     * @param request
     * @param response
     * @param outTradeNo
     */
    @RequestMapping(value="/wechat/paySign",method= RequestMethod.POST)
    public void wechat_paySign(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="outTradeNo",required=true) String outTradeNo
    ) {

        try{

            JsPaySignTool tool= payService.wechat_paySign(request,outTradeNo);
            ResponseMessage.success(tool);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/pay/wechat/paySign----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 线下付款
     * @param request
     * @param response
     * @param outTradeNo
     */
    @RequestMapping(value="/line/paySign",method= RequestMethod.POST)
    public void line_paySign(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="outTradeNo",required=true) String outTradeNo
    ) {

        try{

            payService.line_paySign(request,outTradeNo);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/pay/line/paySign----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 微信回掉
     * @param request
     * @param response
     */
    @RequestMapping(value="/callback/wechat", method = {RequestMethod.POST,RequestMethod.GET})
    public  void callback_wchat(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        try {
            String reuslt =payService.callback_wechat(request);
            AjaxUtils.renderJson(response, reuslt);
        } catch (Exception e) {
            e.printStackTrace();

            logger.error("/sunday/weixin/pay/callback/wechat------"+e.getMessage());
            AjaxUtils.renderJson(response, "fail");
        }
    }

}
